import config from '@/config'
import axios from 'axios'
import Qs from 'qs'
import { getToken } from '@/libs/util';
import { Notification } from "element-ui";

const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro;
const service = axios.create({
    baseURL: baseUrl,
    transformRequest: [function (data, headers) {
        data = Qs.stringify(data);
        headers['Authorization'] =  'Bearer ' + getToken();
        return data
    }],
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json, text/plain, */*',
    }
});

// 响应拦截器
service.interceptors.response.use(response => {
    // 对响应数据做点什么
    return response;
}, error => {
    // 当遇到自定义的错误状态码时，弹出确认框 比如401表示未授权
    if (error.response && error.response.status === 401) {
        Notification({
            title: '系统过期',
            type: 'warning',
            message: '当前访问已过期，请退出系统，并重新登录！！！',
            duration: 3000
        });
    }
    return Promise.reject(error);
});

export default service;
